﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CMS_GYM
{
    public partial class frmThongKeDoanhThuChiPhi : Form
    {
        public frmThongKeDoanhThuChiPhi()
        {
            InitializeComponent();
        }

        private void frmThongKeDoanhThuChiPhi_Load(object sender, EventArgs e)
        {
            try
            {
                txtdTuNgayDT.value = DateTime.Now.AddDays(-30);
                txtDenNgayDT.value = DateTime.Now;
                txtdTuNgayCP.value = DateTime.Now.AddDays(-30);
                txtdDenNgayCP.value = DateTime.Now;
                txtTuNgay.value = new DateTime(DateTime.Now.Year , 1, 1);
                txtDenNgay.value = DateTime.Now;
                KhoiTaoBieuDoDoanhThu();
                KhoiTaoBieuDoChiPhi();
                //KhoiTaoBieuDoDoanhThuChiPhi();
                tbnXemDT_Click(null, null);
                tbnXemCP_Click(null, null);
                tbnXemDT_CP_Click(null, null);
            }
            catch (Exception)
            {

            }
        }

        private void KhoiTaoBieuDoDoanhThu()
        {
            if (chartDoanhThu.Series[0].Points.Count > 0)
            {
                chartDoanhThu.Series[0].Points.Clear();
            }
            System.Windows.Forms.DataVisualization.Charting.DataPoint point = new System.Windows.Forms.DataVisualization.Charting.DataPoint();
            point.IsValueShownAsLabel = false;
            point.XValue = 1;
            point.YValues = new double[] { 1 };
            point.Label = "Doanh thu";
            chartDoanhThu.Series[0].Points.Add(point);
        }

        private void KhoiTaoBieuDoChiPhi()
        {
            if (chartChiPhi.Series[0].Points.Count > 0)
            {
                chartChiPhi.Series[0].Points.Clear();
            }
            System.Windows.Forms.DataVisualization.Charting.DataPoint point = new System.Windows.Forms.DataVisualization.Charting.DataPoint();
            point.IsValueShownAsLabel = false;
            point.XValue = 1;
            point.YValues = new double[] { 1 };
            point.Label = "Chi phí";
            chartChiPhi.Series[0].Points.Add(point);
        }

        private void KhoiTaoBieuDoDoanhThuChiPhi()
        {
            if (chartDT_CP.Series[0].Points.Count > 0)
            {
                chartDT_CP.Series[0].Points.Clear();
            }
            System.Windows.Forms.DataVisualization.Charting.DataPoint point = new System.Windows.Forms.DataVisualization.Charting.DataPoint();
            point.IsValueShownAsLabel = false;
            point.XValue = 1;
            point.YValues = new double[] { 1 };
            point.Label = "Doanh thu - Chi phí";
            chartDT_CP.Series[0].Points.Add(point);
        }

        private void tbnXemDT_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable Tabble = new clsPhieuThuTienBUS().ThongKeDoanhThuTheoLoaiDinhKhoanCo(txtdTuNgayDT.value, txtDenNgayDT.value);
                if (Tabble != null && Tabble.Rows.Count > 0)
                {
                    chartDoanhThu.Series[0].Points.Clear();
                    double TongDoanhThu = 0;
                    for (int i = 0; i < Tabble.Rows.Count; i++)
                    {
                        double DoanhThu = Tabble.Rows[i]["TongTien"] == null || String.IsNullOrEmpty(Tabble.Rows[i]["TongTien"].ToString()) ? 0 : Double.Parse(Tabble.Rows[i]["TongTien"].ToString());
                        TongDoanhThu += DoanhThu;
                    }
                    double TongPhanTram = 0;
                    for (int i = 0; i < Tabble.Rows.Count; i++)
                    {
                        double DoanhThu = Tabble.Rows[i]["TongTien"] == null || String.IsNullOrEmpty(Tabble.Rows[i]["TongTien"].ToString()) ? 0 : Double.Parse(Tabble.Rows[i]["TongTien"].ToString());
                        double PhanTram;
                        if (i == Tabble.Rows.Count - 1)
                        {
                            PhanTram =  Math.Round((100 - TongPhanTram),1);
                        }
                        else
                        {
                            PhanTram = Math.Round((DoanhThu / TongDoanhThu) * 100, 1);
                            TongPhanTram += PhanTram;
                        }
                        System.Windows.Forms.DataVisualization.Charting.DataPoint point = new System.Windows.Forms.DataVisualization.Charting.DataPoint();
                        point.IsValueShownAsLabel = true;
                        point.XValue = DoanhThu;
                        point.YValues = new double[] { DoanhThu };
                        point.Label = Tabble.Rows[i]["TieuDe"] == null || String.IsNullOrEmpty(Tabble.Rows[i]["TieuDe"].ToString()) ? string.Empty : Tabble.Rows[i]["TieuDe"].ToString() + "[" + PhanTram.ToString() + "%]";
                        point.ToolTip = clsSupport.CurrencyNumber(DoanhThu.ToString());
                        point.LabelToolTip = clsSupport.CurrencyNumber(DoanhThu.ToString());
                        chartDoanhThu.Series[0].Points.Add(point);
                    }
                }
                else
                {
                    KhoiTaoBieuDoDoanhThu();
                }

            }
            catch (Exception)
            { }

        }

        private void tbnXemCP_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable Tabble = new clsPhieuChiTienBUS().ThongKeChiPhiTheoLoaiDinhKhoanCo(txtdTuNgayCP.value, txtdDenNgayCP.value);
                if (Tabble != null && Tabble.Rows.Count > 0)
                {
                    chartChiPhi.Series[0].Points.Clear();
                    double TongDoanhThu = 0;
                    for (int i = 0; i < Tabble.Rows.Count; i++)
                    {
                        double DoanhThu = Tabble.Rows[i]["TongTien"] == null || String.IsNullOrEmpty(Tabble.Rows[i]["TongTien"].ToString()) ? 0 : Double.Parse(Tabble.Rows[i]["TongTien"].ToString());
                        TongDoanhThu += DoanhThu;
                    }
                    double TongPhanTram = 0;
                    for (int i = 0; i < Tabble.Rows.Count; i++)
                    {
                        double DoanhThu = Tabble.Rows[i]["TongTien"] == null || String.IsNullOrEmpty(Tabble.Rows[i]["TongTien"].ToString()) ? 0 : Double.Parse(Tabble.Rows[i]["TongTien"].ToString());
                        double PhanTram;
                        if (i == Tabble.Rows.Count - 1)
                        {
                            PhanTram = Math.Round((100 - TongPhanTram), 1);
                        }
                        else
                        {
                            PhanTram = Math.Round((DoanhThu / TongDoanhThu) * 100, 1);
                            TongPhanTram += PhanTram;
                        }
                        System.Windows.Forms.DataVisualization.Charting.DataPoint point = new System.Windows.Forms.DataVisualization.Charting.DataPoint();
                        point.IsValueShownAsLabel = false;
                        point.XValue = DoanhThu;
                        point.YValues = new double[] { DoanhThu };
                        point.Label = Tabble.Rows[i]["TieuDe"] == null || String.IsNullOrEmpty(Tabble.Rows[i]["TieuDe"].ToString()) ? string.Empty : Tabble.Rows[i]["TieuDe"].ToString() + "[" + PhanTram.ToString() + "%]";
                        point.ToolTip = clsSupport.CurrencyNumber(DoanhThu.ToString());
                        point.LabelToolTip = clsSupport.CurrencyNumber(DoanhThu.ToString());
                        chartChiPhi.Series[0].Points.Add(point);
                    }
                }
                else
                {
                    KhoiTaoBieuDoChiPhi();
                }

            }
            catch (Exception)
            { }

        }

        private void tbnXemDT_CP_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable Tabble = new clsPhieuThuTienBUS().ThongKeDoanhThuVaChiPhiTheoNam(txtTuNgay.value, txtDenNgay.value);
                if (Tabble != null && Tabble.Rows.Count > 0)
                {
                    chartDT_CP.Series[0].Points.Clear();
                    double TongDoanhThu = 0;
                    for (int i = 0; i < Tabble.Rows.Count; i++)
                    {
                        double DoanhThu = Tabble.Rows[i]["TongTien"] == null || String.IsNullOrEmpty(Tabble.Rows[i]["TongTien"].ToString()) ? 0 : Double.Parse(Tabble.Rows[i]["TongTien"].ToString());
                        TongDoanhThu += DoanhThu;
                    }
                    double TongPhanTram = 0;
                    for (int i = 0; i < Tabble.Rows.Count; i++)
                    {
                        double DoanhThu = Tabble.Rows[i]["TongTien"] == null || String.IsNullOrEmpty(Tabble.Rows[i]["TongTien"].ToString()) ? 0 : Double.Parse(Tabble.Rows[i]["TongTien"].ToString());
                        double PhanTram;
                        if (i == Tabble.Rows.Count - 1)
                        {
                            PhanTram = Math.Round((100 - TongPhanTram), 1);
                        }
                        else
                        {
                            PhanTram = Math.Round((DoanhThu / TongDoanhThu) * 100, 1);
                            TongPhanTram += PhanTram;
                        }
                        System.Windows.Forms.DataVisualization.Charting.DataPoint point = new System.Windows.Forms.DataVisualization.Charting.DataPoint();
                        point.IsValueShownAsLabel = false;
                        point.XValue = DoanhThu;
                        point.YValues = new double[] { DoanhThu };
                        point.Label = Tabble.Rows[i]["TieuDe"] == null || String.IsNullOrEmpty(Tabble.Rows[i]["TieuDe"].ToString()) ? string.Empty : Tabble.Rows[i]["TieuDe"].ToString() + "[" + PhanTram.ToString() + "%]";
                        point.ToolTip = clsSupport.CurrencyNumber(DoanhThu.ToString());
                        point.LabelToolTip = clsSupport.CurrencyNumber(DoanhThu.ToString());
                        chartDT_CP.Series[0].Points.Add(point);
                    }
                }
                else
                {
                    KhoiTaoBieuDoDoanhThuChiPhi();
                }

            }
            catch (Exception)
            { }

        }
    }
}
